﻿Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Drawing
Imports ajayumi.develop.VBasic.Controls.Forms.Events

Namespace Forms.Renders

    ''' <summary>
    ''' 窗体绘制类的基类
    ''' </summary>
    ''' <remarks></remarks>
    Public MustInherit Class FormRenderBase
        Implements IDisposable


#Region "Variables"

        Private m_Events As EventHandlerList = Nothing

        Private Shared ReadOnly m_CaptionRenderEvent As New Object()
        Private Shared ReadOnly m_BorderRenderEvent As New Object()
        Private Shared ReadOnly m_BackgroundRenderEvent As New Object()
        Private Shared ReadOnly m_ControlBoxRenderEvent As New Object()

#End Region

#Region "Properties"

        Protected ReadOnly Property Events() As EventHandlerList
            Get
                If Me.m_Events Is Nothing Then
                    Me.m_Events = New EventHandlerList()
                End If
                Return Me.m_Events
            End Get
        End Property

        ''' <summary>
        ''' 因子，主要是用于边角
        ''' </summary>
        Public Overridable ReadOnly Property Factor() As Integer
            Get
                Return 2
            End Get
        End Property

#End Region

#Region "Constructors"
        Protected Sub New()

        End Sub
#End Region

#Region "Events"

        ''' <summary>
        ''' 绘制标题
        ''' </summary>
        ''' <remarks></remarks>
        Public Custom Event CaptionRender As Action(Of Object, CaptionRenderEventArgs)
            AddHandler(ByVal value As Action(Of Object, CaptionRenderEventArgs))
                Me.AddHandler(m_CaptionRenderEvent, value)
            End AddHandler

            RemoveHandler(ByVal value As Action(Of Object, CaptionRenderEventArgs))
                Me.RemoveHandler(m_CaptionRenderEvent, value)
            End RemoveHandler

            RaiseEvent(ByVal arg1 As Object, ByVal arg2 As CaptionRenderEventArgs)
                Dim _Handler As Action(Of Object, CaptionRenderEventArgs) = CType(Me.Events(m_CaptionRenderEvent), Action(Of Object, CaptionRenderEventArgs))
                If _Handler IsNot Nothing Then
                    _Handler.Invoke(arg1, arg2)
                End If
            End RaiseEvent
        End Event

        ''' <summary>
        ''' 绘制边框
        ''' </summary>
        ''' <remarks></remarks>
        Public Custom Event BorderRender As Action(Of Object, BorderRenderEventArgs)
            AddHandler(ByVal value As Action(Of Object, BorderRenderEventArgs))
                Me.AddHandler(m_BorderRenderEvent, value)
            End AddHandler

            RemoveHandler(ByVal value As Action(Of Object, BorderRenderEventArgs))
                Me.RemoveHandler(m_BorderRenderEvent, value)
            End RemoveHandler

            RaiseEvent(ByVal arg1 As Object, ByVal arg2 As BorderRenderEventArgs)
                Dim _Handler As Action(Of Object, BorderRenderEventArgs) = CType(Me.Events(m_BorderRenderEvent), Action(Of Object, BorderRenderEventArgs))
                If _Handler IsNot Nothing Then
                    _Handler.Invoke(arg1, arg2)
                End If
            End RaiseEvent
        End Event

        ''' <summary>
        ''' 绘制背景
        ''' </summary>
        ''' <remarks></remarks>
        Public Custom Event BackgroundRender As Action(Of Object, BackgroundRenderEventArgs)
            AddHandler(ByVal value As Action(Of Object, BackgroundRenderEventArgs))
                Me.AddHandler(m_BackgroundRenderEvent, value)
            End AddHandler

            RemoveHandler(ByVal value As Action(Of Object, BackgroundRenderEventArgs))
                Me.RemoveHandler(m_BackgroundRenderEvent, value)
            End RemoveHandler

            RaiseEvent(ByVal arg1 As Object, ByVal arg2 As BackgroundRenderEventArgs)
                Dim _Handler As Action(Of Object, BackgroundRenderEventArgs) = CType(Me.Events(m_BackgroundRenderEvent), Action(Of Object, BackgroundRenderEventArgs))
                If _Handler IsNot Nothing Then
                    _Handler.Invoke(arg1, arg2)
                End If
            End RaiseEvent
        End Event

        ''' <summary>
        ''' 绘制最大化、最小化、关闭按钮
        ''' </summary>
        ''' <remarks></remarks>
        Public Custom Event ControlBoxRender As Action(Of Object, ControlBoxRenderEventArgs)
            AddHandler(ByVal value As Action(Of Object, ControlBoxRenderEventArgs))
                Me.AddHandler(m_ControlBoxRenderEvent, value)
            End AddHandler

            RemoveHandler(ByVal value As Action(Of Object, ControlBoxRenderEventArgs))
                Me.RemoveHandler(m_ControlBoxRenderEvent, value)
            End RemoveHandler

            RaiseEvent(ByVal arg1 As Object, ByVal arg2 As ControlBoxRenderEventArgs)
                Dim _Handler As Action(Of Object, ControlBoxRenderEventArgs) = CType(Me.Events(m_ControlBoxRenderEvent), Action(Of Object, ControlBoxRenderEventArgs))
                If _Handler IsNot Nothing Then
                    _Handler.Invoke(arg1, arg2)
                End If
            End RaiseEvent
        End Event

#End Region

#Region "Methods"

        '[Private]－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－


        '[Protected]－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－
        Protected MustOverride Sub OnCaptionRender(ByVal e As CaptionRenderEventArgs)
        Protected MustOverride Sub OnBorderRender(ByVal e As BorderRenderEventArgs)
        Protected MustOverride Sub OnBackgroundRender(ByVal e As BackgroundRenderEventArgs)
        Protected MustOverride Sub OnControlBoxRender(ByVal e As ControlBoxRenderEventArgs)

        <UIPermission(SecurityAction.Demand, Window:=UIPermissionWindow.AllWindows)> _
        Protected Sub [AddHandler](ByVal key As Object, ByVal value As [Delegate])
            Me.Events.AddHandler(key, value)
        End Sub

        <UIPermission(SecurityAction.Demand, Window:=UIPermissionWindow.AllWindows)> _
        Protected Sub [RemoveHandler](ByVal key As Object, ByVal value As [Delegate])
            Me.Events.RemoveHandler(key, value)
        End Sub

        '[Public]－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－

        ''' <summary>
        ''' 创建由矩形和由路径构成的图形形状的内部。
        ''' </summary>
        ''' <param name="frm"></param>
        ''' <returns></returns>
        Public MustOverride Function CreateRegion(ByVal frm As FormBase) As Region

        ''' <summary>
        ''' 初始化窗体
        ''' </summary>
        ''' <param name="frm"></param>
        Public MustOverride Sub InitForm(ByVal frm As FormBase)

        Public Sub DrawBackground(ByVal e As BackgroundRenderEventArgs)
            Me.OnBackgroundRender(e)
            RaiseEvent BackgroundRender(Me, e)
        End Sub

        Public Sub DrawBorder(ByVal e As BorderRenderEventArgs)
            Me.OnBorderRender(e)
            RaiseEvent BorderRender(Me, e)
        End Sub

        Public Sub DrawCaption(ByVal e As CaptionRenderEventArgs)
            Me.OnCaptionRender(e)
            RaiseEvent CaptionRender(Me, e)
        End Sub

        Public Sub DrawControlBox(ByVal e As ControlBoxRenderEventArgs)
            Me.OnControlBoxRender(e)
            RaiseEvent ControlBoxRender(Me, e)
        End Sub

#End Region



        Private disposedValue As Boolean = False        ' 检测冗余的调用

        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: 释放其他状态(托管对象)。
                End If

                ' TODO: 释放您自己的状态(非托管对象)。
                ' TODO: 将大型字段设置为 null。
                If Me.m_Events IsNot Nothing Then
                    Me.m_Events = Nothing
                End If
            End If
            Me.disposedValue = True
        End Sub

#Region " IDisposable Support "
        ' Visual Basic 添加此代码是为了正确实现可处置模式。
        Public Sub Dispose() Implements IDisposable.Dispose
            ' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean) 中。
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class

End Namespace

